package com.studiofortress.sf.graphics.display;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import javax.swing.JFrame;

/**
 * For creating a full screen display. It will alter the resolution to match the
 * width and height given.
 * 
 * If fullscreen mode or a resolution change cannot be enabled the window will
 * still appear but as a window.
 * 
 * @author Joseph Lenton - JosephLenton@StudioFortress.com
 * @version 20/01/10
 */
public class GraphicsFullScreen extends GraphicsFrame
{
    /**
     * Creates a new window, and then sets it as
     * fullscreen. The title for the window will be
     * blank.
     * @param width The width of the fullscreen mode.
     * @param height The height of the fullscreen mode.
     */
    public GraphicsFullScreen(int width, int height)
    {
        super(width, height);
    }
    
    /**
     * Creates a new window, and then sets it as
     * fullscreen. The title for the window will not
     * be seen, but is available to be set to help with
     * easy code refactoring.
     * @param width The width of the fullscreen mode.
     * @param height The height of the fullscreen mode.
     * @param title The title for the fullscreen window.
     */
    public GraphicsFullScreen(int width, int height, String title)
    {
        super(width, height, title);
    }
    
    /**
     * Creates a new window, and then sets it as
     * fullscreen. The title for the window will not
     * be seen, but is available to be set to help with
     * easy code refactoring. For can also set if the
     * program will end automatically or not.
     * @param width The width of the fullscreen mode.
     * @param height The height of the fullscreen mode.
     * @param title The title for the fullscreen window.
     * @param exitOnClose Set if the program will end when the window is closed.
     */
    public GraphicsFullScreen(int width, int height, String title, boolean exitOnClose)
    {
        super(width, height, title, exitOnClose);
    }
    
    @Override
    public void display()
    {
        final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        final GraphicsDevice device = ge.getDefaultScreenDevice();

        if ( device.isFullScreenSupported() ) {
            final DisplayMode current = device.getDisplayMode();
            final DisplayMode mode = new DisplayMode(
                    getWidth(),
                    getHeight(),
                    current.getBitDepth(),
                    current.getRefreshRate() );

            try {
                final JFrame frame = getContainer();
                
                frame.setUndecorated( true );
                device.setFullScreenWindow( frame );
                device.setDisplayMode( mode );
                
                frame.addWindowListener(new WindowAdapter(){
                    @Override
                    public void windowClosed(WindowEvent e)
                    {
                        device.setDisplayMode( current );
                        device.setFullScreenWindow( null );
                    }
                });

                frame.setVisible(true);
                getCanvas().requestFocus();
                
            // no resolution change
            } catch( UnsupportedOperationException ex ) {
                device.setDisplayMode( current );
                device.setFullScreenWindow( null );

                super.display();
            }
        // no fullscreen
        } else {
            super.display();
        }
    }
}
